<?php
/**
 * 更新备货非FBA库存
 */
namespace App\Logic\SsBasic;

use App\Models\Inventory\AreaCn;
use App\Models\OverSeasBa\DropshippingInventoryFnsku;
use App\Models\OverSeasBa\InventoryBoxFNSku;
use App\Models\OverSeasBa\RemovaleInventoryFNSku;
use App\Models\OverSeasBa\Warehouse;
use App\Models\SsBasic\AreaSkuStock;
use App\Models\Stocking\StockingSku;
use Illuminate\Support\Facades\DB;
class InitNotFBAStock
{
    /**
     * 设定：每隔*分钟运行 （ss:not-fba-stock）
     */
    public static function exe(){
        try{
            $warehouse = [];
            //备货sku
            StockingSku::query()->orderBy('id')
                ->chunk(500, function ($ss) use (&$warehouse){
                    DB::beginTransaction();
                    foreach ($ss as $_ss){
                        $instant = AreaCn::getInstant($_ss->area_id,$_ss->psku_id);
                        $transit = AreaCn::getTransit($_ss->area_id,$_ss->psku_id);
                        if(!array_key_exists($_ss->area_id,$warehouse)){
                            $warehouse[$_ss->area_id] = Warehouse::getIdsByAreaId($_ss->area_id);
                        }
                        $warehouseIds = $warehouse[$_ss->area_id];
                        $osInventory1 = InventoryBoxFNSku::getInstantAndTransit($warehouseIds,$_ss->psku_id);
                        $osInventory2 = RemovaleInventoryFNSku::getInstantAndTransit($warehouseIds,$_ss->psku_id);
                        $osInventory3 = DropshippingInventoryFnsku::getInstantAndTransit($warehouseIds,$_ss->psku_id);

                        //国内在库、在途
                        $_ss->cn_instant_num = $instant;
                        $_ss->cn_transit_num = $transit;

                        //海外在库、在途
                        $_ss->os_instant_num = ($osInventory1->instant ?? 0) + ($osInventory2->instant ?? 0) + ($osInventory3->instant ?? 0);
                        $_ss->os_transit_num = ($osInventory1->transit ?? 0) + ($osInventory2->transit ?? 0) + ($osInventory3->transit ?? 0);
                        $_ss->save();

                        $_ss->over_stock = StockingSku::getOverStock($_ss);
                        $_ss->over_stock_value = StockingSku::getOverStockValue($_ss->over_stock,$_ss->unit_price);
                        $_ss->save();

                        //备货站点sku库存（国内和海外）
                        $stock = AreaSkuStock::query()->firstOrNew(['area_psku' => $_ss->area_psku]);
                        $stock->cn_instant_num = $instant;
                        $stock->cn_transit_num = $transit;
                        $stock->os_instant_num = ($osInventory1->instant ?? 0) + ($osInventory2->instant ?? 0) + ($osInventory3->instant ?? 0);
                        $stock->os_transit_num = ($osInventory1->transit ?? 0) + ($osInventory2->transit ?? 0) + ($osInventory3->transit ?? 0);
                        $stock->save();
                    }
                    DB::commit();
                });
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
        }
    }
}
